package xyz.scootaloo.oj.offer

import org.junit.Test
import xyz.scootaloo.oj.ds.TreeNode
import xyz.scootaloo.oj.ds.helper.CollectionBuilder

/**
 * @author flutterdash@qq.com
 * @since 2021/4/10 22:53
 */
class P32I {

    private fun levelOrder(root: TreeNode?): IntArray {
        if (root == null) return IntArray(0)
        val deque = ArrayDeque<TreeNode>()
        val res = ArrayList<Int>()

        deque.addLast(root)
        while (!deque.isEmpty()) {
            val current = deque.removeFirst()
            res.add(current.`val`)
            if (current.left != null) deque.addLast(current.left)
            if (current.right != null) deque.addLast(current.right)
        }

        return res.toIntArray()
    }

    @Test
    fun test0() {
        val tree = CollectionBuilder.initTree()
            .setRoot(3)
            .addLevel(9, 20)
            .addLevel(null, null, 15, 7)
            .build()
        println(levelOrder(tree).contentToString())
    }

}
